package com.learn.kotlinlearndemolist.ui

import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.learn.kotlinlearndemolist.R
import com.learn.kotlinlearndemolist.databinding.FragmentLeftBinding
import com.learn.kotlinlearndemolist.model.DataBean
import com.learn.kotlinlearndemolist.model.HighFun.lettersCount
import com.learn.kotlinlearndemolist.model.HigherOrderFunction
import com.learn.kotlinlearndemolist.receiver.TimeChangeReceiver
import com.learn.kotlinlearndemolist.service.DetectionService
import com.learn.kotlinlearndemolist.view.CustomDialog


class LeftFragment : Fragment() {
    private lateinit var mBinding: FragmentLeftBinding
    private lateinit var timeChangeReceiver: TimeChangeReceiver
    private lateinit var intentDetection: Intent

    companion object {
        const val TAG = "LeftFragment"
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(requireContext()),
            R.layout.fragment_left,
            container,
            false
        )
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val count = "ABCDE12313xyz@#$$@!".lettersCount()
        mBinding.highFunText.text = "$count"
        val dataBean1 = DataBean(1, "小明")
        val dataBean2 = DataBean(2, "小红")
        val dataBean = dataBean1 + dataBean2
        mBinding.operatorFunText.text = dataBean.toString()
        val intentFilter = IntentFilter()
        intentFilter.addAction("android.intent.action.TIME_TICK")
        timeChangeReceiver = TimeChangeReceiver()
        requireContext().registerReceiver(timeChangeReceiver, intentFilter)
        intentDetection = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
        intentDetection.setClass(requireContext(), DetectionService::class.java)
        requireContext().startService(intentDetection)
        val customDialog = CustomDialog(requireContext())
        mBinding.sendBroadcast.setOnClickListener {
            val intent = Intent("com.kotlin.demo.custom.broadcast")
            intent.setPackage(requireContext().packageName)
//            requireContext().sendBroadcast(intent)
            requireContext().sendOrderedBroadcast(intent, null)
            customDialog.show()
        }

        val number1 = 100
        val number2 = 50
        val result1 = HigherOrderFunction.num1AndNum2(number1, number2, ::plus)
        val result2 = HigherOrderFunction.num1AndNum2(number1, number2, ::minus)
        Log.d(TAG, "onViewCreated: $result1")
        Log.d(TAG, "onViewCreated: $result2")
    }


    fun plus(num1: Int, num2: Int): Int {
        return num1 + num2
    }

    fun minus(num1: Int, num2: Int): Int {
        return num1 - num2
    }

    override fun onDestroy() {
        super.onDestroy()
        requireContext().unregisterReceiver(timeChangeReceiver)
        requireContext().stopService(intentDetection)
    }
}